package com.example.question;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Random;

public class CreatQandA {
    private int total;
    private ArrayList<QandaType> qandaTypeList;
    private QuestionType questionType;


    public CreatQandA(QuestionType questionType) {
        this.questionType = questionType;
        qandaTypeList=new ArrayList<>();
    }

    public void start() {
        int weightAll = 0;
        total = questionType.getTotal();
        int typeNum = questionType.getQuestionList().size();

        ArrayList<Type> questionList = questionType.getQuestionList();
        for (int i = 0; i < typeNum; i++) {
            weightAll += questionList.get(i).getWeight();

        }
        int typeCnt=0;
        int typeTotal=0;
        qandaTypeList.clear();//这里这个很关键，每次启动start时这个列表都要清空这样就可以刷新题目了。
        for (int i = 0; i < typeNum; i++) {

            double rate = questionList.get(i).getWeight() / (double)weightAll;
            typeTotal+=typeCnt;
            typeCnt = (int) Math.floor(rate * total);
            if(i==typeNum-1){
                typeCnt=total-typeTotal;
            }
            for (int j = 0; j < typeCnt; j++) {
                QuestiontypeEnum questiontypeEnum = questionList.get(i).getQuestiontypeEnum();
                switch (questiontypeEnum) {
                    case TEN_ADD:
                        qandaTypeList.add(Ten_Add());
                        break;
                    case TEN_SUB:
                        qandaTypeList.add(Ten_Sub());
                        break;
                    case TWENTY_SUB:
                        qandaTypeList.add(Twenty_Sub());
                        break;
                    case TWENTY_ADD_SUB:
                        qandaTypeList.add(Twenty_Add_Sub());
                        break;
                    case HUNDRED_ADD:
                        qandaTypeList.add(Hundred_Add());
                        break;
                    case HUNDRED_SUB:
                        qandaTypeList.add(Hundred_Sub());
                        break;
                    case HUNDRED_ADD_SUB:
                        qandaTypeList.add(Hundred_Add_Sub());
                        break;

                }
            }
        }
        Collections.shuffle(qandaTypeList);//打乱题目排序


    }

    public int getTotal() {
        return total;
    }

    public ArrayList<QandaType> getQandaTypeList() {
        return qandaTypeList;
    }

    private QandaType Ten_Add() {
        QandaType qandaType;
        String question;
        int add0 = MyRand(0, 9);
        int add1 = MyRand(0, 9);
        int answer = add0 + add1;
        question = add0 + "+" + add1 + "=";
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance() );
        return qandaType;
    }

    private QandaType Ten_Sub() {
        QandaType qandaType;
        String question;
        int sub0 = MyRand(1, 10);
        int sub1 = MyRand(0, sub0);
        int answer = sub0 - sub1;
        question = sub0 + "-" + sub1 + "=";
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance());
        return qandaType;
    }

    private QandaType Twenty_Sub() {
        QandaType qandaType;
        String question;
        int sub0 = MyRand(11, 19);
        int sub1 = MyRand(sub0-11, 9);
        int answer = sub0 - sub1;
        question = sub0 + "-" + sub1 + "=";
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance());
        return qandaType;
    }

    private QandaType Twenty_Add_Sub() {
        QandaType qandaType;
        String question;
        int sig = MyRand(0, 1);//0 = + ；1 = -
        int a;
        int b;
        int c;
        int answer;
        if (sig == 0) {
            a = MyRand(1, 9);
            b = MyRand(10-a, 9);
            c = MyRand(0, Math.min(a + b, 9));
            answer = a + b - c;
            question = a + "+" + b + "-" + c + "=";
        } else {
            a = MyRand(11, 19);
            b = MyRand(a-11, 9);
            c = MyRand(0, 19 -a+ b);
            answer = a - b + c;
            question = a + "-" + b + "+" + c + "=";
        }
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance());
        return qandaType;
    }

    private QandaType Hundred_Add() {
        QandaType qandaType;
        String question;
        int add0 = MyRand(10, 99);
        int add1 = MyRand(10, 99);
        int answer = add0 + add1;
        question = add0 + "+" + add1 + "=";
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance());
        return qandaType;
    }

    private QandaType Hundred_Sub() {
        QandaType qandaType;
        String question;
        int sub0 = MyRand(11, 99);
        int sub1 = MyRand(0, sub0);
        int answer = sub0 - sub1;
        question = sub0 + "-" + sub1 + "=";
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance());
        return qandaType;
    }
    private QandaType Hundred_Add_Sub() {
        QandaType qandaType;
        String question;
        int sig = MyRand(0, 1);//0 = + ；1 = -
        int a;
        int b;
        int c;
        int answer;
        if (sig == 0) {
            a = MyRand(11, 99);
            b = MyRand(11, 99);
            c = MyRand(0, Math.min(a + b, 99));
            answer = a + b - c;
            question = a + "+" + b + "-" + c + "=";
        } else {
            a = MyRand(11, 99);
            b = MyRand(0, a);
            c = MyRand(0, 99);
            answer = a - b + c;
            question = a + "-" + b + "+" + c + "=";
        }
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance());
        return qandaType;
    }
    private QandaType Moneybig(){
        QandaType qandaType;
        String question;
        int answer;
        int yuan;
        int jiao;
        yuan=MyRand(1,9);
        jiao=MyRand(0,9);
        question=yuan+"元"+jiao+"角"+"=  角";
        answer=yuan*10+jiao;
        qandaType = new QandaType(question, String.valueOf(answer), Calendar.getInstance());
        return qandaType;
    }
    private QandaType Moneysmall(){
        QandaType qandaType;
        String question;
        int fen;
        int jiao;
        int yuan;
        String answer;
        int sin=MyRand(0,1);//0=fen 1=jiao
        if(sin==0){
            fen=MyRand(10,900);
            yuan=fen/100;
            jiao=fen%100/10;
            fen=fen%10;
            question=fen+"分=?元?角?分";
            answer=yuan+"*"+jiao+"*"+fen;
        }
        else{
            jiao=MyRand(10,90);
            yuan=jiao/10;
            jiao=jiao%10;
            question=jiao+"角=?元?角";
            answer=yuan+"*"+jiao;
        }
        qandaType = new QandaType(question, answer, Calendar.getInstance());
        return qandaType;
    }

    private int MyRand(int min, int max) {
        Random rand = new Random();
        return rand.nextInt(max - min + 1) + min;
    }

}


